package com.lmt.config.shiro.filter.authz;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 只要拥有任何一个权限即可
 * 一个权限：anyPermission[permission1]
 * 多个权限中的一个：anyPermission["permission1,permission2"]
 */
public class AnyPermissionAuthorizationFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        Subject subject = getSubject(request, response);
        String[] perms = (String[]) mappedValue;

        if (perms == null || perms.length <= 0) {
            return true;
        }

        boolean isPermitted = false;

        for (String perm : perms) {
            if (subject.isPermitted(perm)) {
                isPermitted = true;
                break;
            }
        }

        return isPermitted;
    }
}
